<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<body>

<script type="text/javascript">
$(function() {
	djt_S_editIndex = undefined;
	
	var Sobid=<%=request.getAttribute("Sobid")%>;
	//console.log("Sobid"+Sobid);
	
	$('#djt_S_dg'+Sobid+'').datagrid({
    	url:'../salaryService/findSByPage?id='+Sobid+'',
    	columns:[[
    		{field:'',title:'',checkbox:true},
			{field:'salaryId',title:'编号',width:'60',sortable:true},
			{field:'salaryName',title:'薪资表名',width:'100',
				formatter: function(value,row,index){
					return '<a href="#" onclick="addTab(&#39;薪资详情表：'+row.salaryName+'&#39;, &#39;../salary/salary_details?salaryId='+row.salaryId+'&#39;)">'+row.salaryName+'</a>'
				},
				editor:{type:'text',options:{required:true}}},
			{field:'salaryBeginDate',title:'工资计算月份',width:'115',editor:{type:'datebox',options:{required:true,tipPosition:'right'}}},
			{field:'countStatus',title:'计算状态',width:'100',
				formatter: function(value,row,index){
					 if (row.countStatus==1){
						return '已计算';
					} else {
						return '未计算';
					} 
				}},
			{field:'remark',title:'备注',width:'100',editor:{type:'text'}},
		]],
		fit:true,
 	   	fitColumns:false,
 	   	//隔行变色
 	   	striped:true,
  	  	nowrap:true,
		toolbar:'#Stb'+Sobid+'',
		pagination:true,
		pageSize:5,
		pageList:[5,10,20,40],
		border:false,
		sortName:'salaryId',
		sortOrder:'asc',
		onAfterEdit : function(rowIndex,rowData,changes){
			var inserted = $('#djt_S_dg'+Sobid+'').datagrid('getChanges','inserted');
			var updated = $('#djt_S_dg'+Sobid+'').datagrid('getChanges','updated');
			var url = '';
			if(inserted.length > 0){
				url = '../salaryService/insertS?salarySobId='+Sobid;
			}
			if(updated.length > 0){
				url = '../salaryService/updateS';
			}
			var djt_S_rowData = JSON.stringify(rowData);
			$.post(url,{djt_S_rowData},function(e){
				var info = JSON.parse(e);
				//console.log(info);
				if(info.success){
					$('#djt_S_dg'+Sobid+'').datagrid('acceptChanges');
					$.messager.show({
						title:'成功',
						msg:info.msg,
					});
				}else{
					$('#djt_S_dg'+Sobid+'').datagrid('rejectChanges');
					$.messager.alert('错误',info.msg,'error');
				}
			})
			djt_S_editIndex = undefined;
			$('#djt_S_dg'+Sobid+'').datagrid('unselectAll');
		},
		onDblClickRow : function(rowIndex,rowData){
			if(djt_S_editIndex != undefined){
				$('#djt_S_dg'+Sobid+'').datagrid('endEdit',djt_S_editIndex);
				djt_S_editIndex = undefined;
			}
			if(djt_S_editIndex == undefined){
				$('#djt_S_dg'+Sobid+'').datagrid('beginEdit',rowIndex);
				djt_S_editIndex = rowIndex;
			}
		}
	});

});

//保存
function saveS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	$('#djt_S_dg'+Sobid+'').datagrid('endEdit',djt_S_editIndex);
}

//删除
function removeS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_S_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length > 0){
		$.messager.confirm('请确认','您确定要删除当前所有选中的项目吗？',function(r){
			if(r){
				var ids = [];
				for(var i = 0; i < rows.length; i++){
					ids.push(rows[i].salaryId)
				}
				var djt_S_rowData = JSON.stringify(ids);
				$.post('../salaryService/deleteS',{djt_S_rowData},function(e){
					var info = JSON.parse(e);
					if(info.success){
						$('#djt_S_dg'+Sobid+'').datagrid('load');
						$('#djt_S_dg'+Sobid+'').datagrid('unselectAll');
						$.messager.show({
							title:'成功',
							msg:info.msg,
						});
					}else{
						$('#djt_S_dg'+Sobid+'').datagrid('rejectChanges');
						$.messager.alert('错误',info.msg,'error');
					}
				})
				djt_S_editIndex = undefined;
			}
		})
	}else{
		$.messager.alert('提示','请选择要删除的记录！','error');
	}
}

//编辑
function editS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	var rows = $('#djt_S_dg'+Sobid+'').datagrid('getSelections');
	if(rows.length == 1){
		if(djt_S_editIndex != undefined){
			$('#djt_S_dg'+Sobid+'').datagrid('endEdit',djt_S_editIndex);
		}
		if(djt_S_editIndex == undefined){
			var index = $('#djt_S_dg'+Sobid+'').datagrid('getRowIndex',rows[0]);
			$('#djt_S_dg'+Sobid+'').datagrid('beginEdit',index);
			djt_S_editIndex = index;
			$('#djt_S_dg'+Sobid+'').datagrid('unselectAll');
		}
	}else{
		$.messager.show({
			title:'提示',
			msg:'一次只能编辑一行！',
		});
	}
}

//撤销编辑
function rejectS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	djt_S_editIndex = undefined;
	$('#djt_S_dg'+Sobid+'').datagrid('rejectChanges');
	$('#djt_S_dg'+Sobid+'').datagrid('unselectAll');
}

//开启添加
function addS(){
	var Sobid=<%=request.getAttribute("Sobid")%>;
	if(djt_S_editIndex != undefined){
		$('#djt_S_dg'+Sobid+'').datagrid('endEdit',djt_S_editIndex);
	}
	if(djt_S_editIndex == undefined){
		$('#djt_S_dg'+Sobid+'').datagrid('insertRow',{
			index : 0,
			row:{
				
			}
		});
		$('#djt_S_dg'+Sobid+'').datagrid('beginEdit',0);
		djt_S_editIndex = 0;
	}
}
</script>

<div class="easyui-layout" fit="true" border="false">
	<div region="center" border="false">
		<table id="djt_S_dg${Sobid}"></table>
	
		<div id="Stb${Sobid}">
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addS()">添加</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeS()">删除</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editS()">编辑</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveS()">保存</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="rejectS()">撤销编辑</a>
		</div>
	</div>	
</div>
</body>